package com.tview.visual;

import static com.tview.visual.Spectrum.VLC_CODEC_S16N;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.media.AudioTimestamp;
import android.media.AudioTrack;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;

public class MainActivity3 extends AppCompatActivity {

    private static final String[] PERMISSIONS = new String[]{
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.MODIFY_AUDIO_SETTINGS,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    private File mFile;
    private String TAG = "tag";
    private MediaExtractor mExtractor;
    private boolean runing = true;

    Spectrum spectrum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityCompat.requestPermissions(this, PERMISSIONS, 1);
        setContentView(R.layout.activity_main3);
        Log.e(TAG, "onCreate: "+VLC_CODEC_S16N);
        spectrum = new Spectrum(2,1,VLC_CODEC_S16N);
        initMeidia();
    }

    private void initMeidia() {
        mFile = new File("/sdcard/out.wav");
//        mFile = new File("/sdcard/心痛08744.mkv");
        if (!mFile.exists()) {
            Log.e(TAG, "mp4文件不存在");
            return;
        }
        mExtractor = new MediaExtractor();//实例一个MediaExtractor
        try {
            mExtractor.setDataSource(mFile.getAbsolutePath());//设置添加MP4文件路径
        } catch (IOException e) {
            e.printStackTrace();
        }
        int count = mExtractor.getTrackCount();//获取轨道数量
        Log.e(TAG, "轨道数量 = " + count);
        for (int i = 0; i < count; i++) {
            MediaFormat mediaFormat = mExtractor.getTrackFormat(i);
            Log.e(TAG, i + "编号通道格式 = " + mediaFormat.getString(MediaFormat.KEY_MIME));
            if (mediaFormat.getString(MediaFormat.KEY_MIME).contains("audio")) {
                mExtractor.selectTrack(i);
                Log.e(TAG, mediaFormat.toString());

            }
        }

        int[] ints = new int[80];
//        mExtractor.seekTo(10000000,SEEK_TO_CLOSEST_SYNC);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                while (runing) {
                    int size = mExtractor.readSampleData(byteBuffer, 0);
                    if (size == -1)
                        return;

                    Log.e(TAG, "run: "+ size);
                    spectrum.pushData(byteBuffer.array(),size/2,ints);

                    StringBuilder builder = new StringBuilder();
                    for (int i : ints){
                        builder.append(", "+i);
                    }
                    Log.e(TAG, "run: "+builder.toString());
//                    byte buf[] = new byte[size];
//                    System.arraycopy(byteBuffer.array(), 0, buf, 0, size);

                    mExtractor.advance();
                }
            }
        });
        thread.start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        runing = false;
    }
}